<?php

/**
 * Maikuro CMS
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * @see AbstractApplicationContext
 */
require_once 'AbstractApplicationContext.php';

/**
 * @see TagParser
 */
require_once 'tag/TagParser.php';

/**
 * 
 */
class FrontendApplicationContext extends AbstractApplicationContext
{
    /**
     * The current page (if any)
     *
     * @var PageEntity
     */
    private $currentPage;

    /**
     * The current site.
     * 
     * @var SiteEntity
     */
    private $currentSite;

    /**
     * The template parser.
     *
     * @var TagParser
     */
    private $tagParser;

    /**
     * Returns the current page (if any).
     *
     * @return PageEntity
     */
    public function getCurrentPage()
    {
        return $this->currentPage;
    }

    /**
     * Returns the current site.
     *
     * @return SiteEntity
     */
    public function getCurrentSite()
    {
        return $this->currentSite;
    }

    /**
     * Returns the session user.
     *
     * @var SessionUser
     */
    public function getSessionUser()
    {
        $sessionUser = Session::get(Constants::SESSION_USER);
        if ($sessionUser == null) {
            Session::set(Constants::SESSION_USER, new SessionUser());
        }
        return $sessionUser;
    }

    /**
     * Returns the template parser.
     *
     * @return TagParser
     */
    public function getTagParser()
    {
        if ($this->tagParser === null) {
            $this->tagParser = new TagParser();
        }
        return $this->tagParser;
    }

    /**
     * Checks if there is a current page.
     *
     * @return boolean
     */
    public function hasCurrentPage()
    {
        return ($this->currentPage !== null);
    }

    /**
     * Loads any frontend plugins.
     *
     * @return FrontendApplicationContext
     * /
    protected function loadPlugins()
    {
        $plugins = $this->serviceFactory->getPluginService()->getPlugins();
        foreach ($plugins as $plugin) {
            if (!$plugin->isEnabled || !$plugin->isContentPlugin) {
                continue;
            }
            $pluginName = $plugin->name;
            require_once $this->getBasePath() . '/plugins/' . $pluginName . '/' . $pluginName . '.php';
            $this->plugins[$pluginName] = new $pluginName($this);
        }
        return $this;
    }
     * 
     */

    /**
     * Sets the current page.
     *
     * @param PageEntity $page
     * @return FrontendApplicationContext
     */
    public function setCurrentPage(PageEntity $page)
    {
        $this->currentPage = $page;
        return $this;
    }

    /**
     * Sets the current site.
     *
     * @param SiteEntity $site
     * @return FrontendApplicationContext
     */
    public function setCurrentSite(SiteEntity $site)
    {
        $this->currentSite = $site;
        return $this;
    }
}